package www.ym.com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.entity.Department;
import www.ym.com.entity.Role;
import www.ym.com.entity.User;
import www.ym.com.service.DepartmentService;
import www.ym.com.service.RoleService;
import www.ym.com.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 员工职位下拉框
     *
     * @return
     */
    @Transactional
    @GetMapping("/list")
    public R<JSONArray> list() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.notIn(Role::getRoleName, "超级管理员", "管理员");
        List<Role> list = roleService.list(queryWrapper);

        ArrayList<String> repeatList1 = new ArrayList<>();
        for (int i = 0; i < list.toArray().length; i++) {
            repeatList1.add(String.valueOf(list.get(i).getDepartmentName()));
        }
        ArrayList<String> list1 = new ArrayList<>();
        for (int i = 0; i < repeatList1.toArray().length; i++) {
            if (!list1.contains(repeatList1.get(i))) {
                list1.add(repeatList1.get(i));
            }
        }
        log.info("list1" + list1);
        JSONArray option = new JSONArray();
        for (int j = 0; j < list1.toArray().length; j++) {
            Map<String, Object> dpt = new HashMap<>();
            JSONArray children = new JSONArray();
            LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Role::getDepartmentName, list1.get(j));
            log.info("i+"+list1.get(j));
            List<Role> roles = roleService.list(wrapper);
            dpt.put("label",list1.get(j));
            LambdaQueryWrapper<Department> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Department::getDepartmentName,list1.get(j));
            Department department = departmentService.getOne(wrapper1);
            dpt.put("value",department.getId());
            dpt.put("children", children);
            children.clear();
            for (int f = 0; f < roles.toArray().length; f++) {
                Map<String, Object> childrenValue = new HashMap<>();
                childrenValue.put("label", roles.get(f).getRoleName());
                childrenValue.put("value", roles.get(f).getId());
//                log.info("children" + children);
                JSONObject json1 = new JSONObject(childrenValue);
                children.add(json1);
            }
            JSONObject jsonObject = new JSONObject(dpt);
            option.add((jsonObject));
//            log.info("option" + option);
        }
        log.info("option" + option);
        return R.success(option, null);
    }


    /**
     * 分页查询
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/list")
    public RL<Role> page(@RequestBody JSONObject jsonObject) {
        Page<Role> userPage = roleService.pageUser(jsonObject);
        List<Role> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = userPage.getCurrent();
        long pageSize = userPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }

    /**
     * 增加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public R<String> addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return R.success(null, "添加成功");
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @PostMapping("/update")
    public R<String> updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return R.success(null, "修改成功");
    }

    /**
     * 删除角色
     *
     * @param role
     * @return
     */
    @PostMapping("/del")
    public R<String> del(@RequestBody Role role) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Role::getId,role.getId());
//        Role role1 = roleService.getOne(queryWrapper);

//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(User::getRoleName,role1.getRoleName());
//        int count = userService.count(wrapper);
//        if (count>0){
//            throw new CustomException("角色已绑定人员，无法删除");
//        }
//
//        if ("超级管理员".equals(role1.getRoleName())) {
//            throw new CustomException("超级管理员无法删除");
//        }
        roleService.removeById(role.getId());
        return R.success(null, "删除成功");
    }

}
